package com.example.bigevent.controller;


import com.example.bigevent.pojo.Category;
import com.example.bigevent.pojo.Result;
import com.example.bigevent.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-05-30
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    /**
     * 新增文章分类
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated Category category){
        log.info("新增文章分类：{}",category);
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 文章分类列表
     * @return
     */
    @GetMapping
    public Result<List<Category>> listCategory(){
        List<Category> list = categoryService.listCategory();
        return Result.success(list);
    }

    /**
     * 获取文章分类详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Category> categoryDetail(Integer id){
        return Result.success(categoryService.getById(id));
    }

    /**
     * 更新文章分类
     * @param category
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated Category category){
        categoryService.updateCategory(category);
        return Result.success();
    }

    /**
     * 删除文章分类
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id){
        categoryService.removeById(id);
        return Result.success();
    }
}
